<template>
  <view
    class="content"
    v-if="banners.length">
    <!-- 导航 -->
    <nav-bar></nav-bar>
    <scroll-view
      class="main"
      scroll-y
      refresher-enabled
      @scrolltolower="scrollToBottom"
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="refresherTriggered">
      <!-- 广告 -->
      <carousel
        :banners="banners"
        height="280rpx"></carousel>
      <!-- 分类 -->
      <cate-scroll :categoryHeadMutli="categoryHeadMutli"></cate-scroll>
      <!-- 推荐 -->
      <view class="panel recommend">
        <view
          class="item"
          v-for="item in hotMutli"
          :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards">
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"></image>
          </navigator>
        </view>
      </view>
      <!-- 好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in homeNew"
            :key="item.id">
            <image
              mode="aspectFit"
              :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price"> <text class="small">¥</text>{{ item.price }} </view>
          </navigator>
        </view>
      </view>
      <!-- 品牌 -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator
            hover-class="none"
            class="more"
            url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator
            hover-class="none"
            url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator
            hover-class="none"
            url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator
            hover-class="none"
            url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <!-- 猜你喜欢 -->
      <Guess
        :homeGoodsGuessLike="guesslikeList"
        :hasMore="hasMore"></Guess>
    </scroll-view>
  </view>
  <view v-else>
    <skeleton></skeleton>
  </view>
</template>

<script>
import Carousel from '@/components/Carousel/Carousel.vue'
import navBar from './components/Navbar.vue'
import skeleton from './skeleton/index.vue'
import {
  getHomeBanner,
  homeCategoryHeadMutli,
  getHomeHotMutli,
  getHomeNew,
  getHomeGoodsGuesslike
} from '@/http/home'
import CateScroll from '@/components/CateScroll/CateScroll.vue'
export default {
  components: {
    'nav-bar': navBar,
    Carousel,
    CateScroll,
    skeleton
  },
  data() {
    return {
      banners: [],
      // 分类
      categoryHeadMutli: [],
      // 推荐
      hotMutli: [],
      // 新鲜好物
      homeNew: [],
      // 猜你喜欢的参数
      // guessParams: {
      //   page: 1,
      //   pageSize: 10
      // },
      // // 猜你喜欢总页数
      // guessPages: 0,
      // 猜你喜欢存放数组
      guesslikeList: [],
      // 是否还有数据
      hasMore: true,
      // 控制下拉刷新
      refresherTriggered: false
    }
  },
  async onLoad() {
    this.loadData()
  },
  methods: {
    loadData() {
      // 猜你喜欢的参数
      this.guessParams = {
        page: 1,
        pageSize: 10
      }
      // 猜你喜欢总页数
      this.guessPages = 1

      /**
       * 当前业务需求
       * 1.需要同时发送请求， 但是不需要同时接收数据，所以不需要.then 去赋值
       * 2.需要返回的结果 是promise， 因为下面的业务，需要同步操作
       * 3.所以使用了 promise.all
       */
      return Promise.all([
        this.getHomeBanner(),
        this.homeCategoryHeadMutli(),
        this.getHomeHotMutli(),
        this.getHomeNew(),
        this.getHomeGoodsGuesslike(this.guessParams)
      ])
    },
    async getHomeBanner() {
      const res = await getHomeBanner()
      this.banners = res.result
      console.log(2)
    },

    async homeCategoryHeadMutli() {
      const res = await homeCategoryHeadMutli()
      this.categoryHeadMutli = res.result
      console.log(3)
    },

    async getHomeHotMutli() {
      const res = await getHomeHotMutli()
      console.log('tuijian', res)
      this.hotMutli = res.result
    },

    async getHomeNew() {
      const res = await getHomeNew()
      this.homeNew = res.result
    },

    async getHomeGoodsGuesslike(params) {
      const res = await getHomeGoodsGuesslike(params)
      this.guessPages = res.result.pages
      this.GuesslikeList = res.result.items
    },

    async scrollToBottom() {
      if (this.guessParams.page >= this.guessPages) {
        uni.showToast({ title: '没有更多数据了', icon: 'none' })
        this.hasMore = false
      } else {
        this.guessParams.page++
        const res = await getHomeGoodsGuesslike(this.guessParams)
        this.guesslikeList = [...this.guesslikeList, ...res.result.items]
      }
    },
    async onRefresherrefresh() {
      this.refresherTriggered = true
      // 重置数据
      this.banners = []
      this.categoryHeadMutli = []
      this.hotMutli = []
      this.homeNew = []
      this.guesslikeList = []
      this.hasMore = true

      console.log(1)
      // 重新发送请求
      await this.loadData()
      console.log(6)
      // 关闭下拉刷新
      this.refresherTriggered = false
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}

/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;

  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;

    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }

  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }

  .cards {
    display: flex;
  }

  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;

  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;

    &:nth-child(2n) {
      border-right: 0 none;
    }

    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }

  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;

    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;

  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }

  .small {
    font-size: 80%;
  }
}

.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}

.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}

.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}

.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
